﻿using UnityEngine;
using System.Collections;
using System.IO;
using System;

public class CopyFile : MonoBehaviour {

    public string originFile = "D:/Data.txt";
    public string targetFile = "D:/Data_Copy.txt";
    void Start () {
        Copy(originFile,targetFile);
    }
    void Copy(string origin,string target)
    {
        using (FileStream streamOrigin = new FileStream(origin, FileMode.Open, FileAccess.Read))
        {
            using (FileStream streamTarget = new FileStream(target, FileMode.Create, FileAccess.Write))
            {
                byte[] buffer = new byte[1024];

                int realRead = streamOrigin.Read(buffer, 0, buffer.Length);

                //计算百分比
                long total = streamOrigin.Length;
                long currentLength = realRead;

                while (realRead > 0)
                {
                    streamTarget.Write(buffer, 0, realRead);
                    //清空缓存区，防止脏读
                    Array.Clear(buffer, 0, buffer.Length);

                    realRead = streamOrigin.Read(buffer, 0, buffer.Length);
                    currentLength += realRead;

                    float percent = currentLength*100 / total;
                    Debug.Log("拷贝进度：" + percent + "%");
                }

                Debug.Log("拷贝完毕。。。");
            }
        };
    }
}
